home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Libraries / TurboTCP 2.0.1 / TurboTCP source / CTCPEndpoint.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-10  |  7.8 KB  |  261 lines  |  [TEXT/MPCC]

  1. /*
  2. ** CTCPEndpoint.h
  3. **
  4. **    TurboTCP support library
  5. **    TCP generic protocol interpreter
  6. **
  7. **    Copyright © 1993-94, FrostByte Design / Eric Scouten
  8. **
  9. */
  10.  
  11.  
  12. #pragma once
  13.  
  14. #include "TCL.h"
  15. #include "UTurboTCP.h"
  16.  
  17. class CTCPStream;
  18. class CTCPResolverCall;
  19.  
  20.  
  21. // TCP precedence values (used for SetPrecedence() method)_
  22.  
  23. enum TCPPrecedence {
  24.     precRoutine = 0,
  25.     precPriority = 1,
  26.     precImmediate = 2,
  27.     precFlash = 3,
  28.     precFlashOverride = 4,
  29.     precCriticECP = 5,
  30.     precInetControl = 6,
  31.     precNetControl = 7
  32. };
  33.  
  34. // ICMP report message types
  35.  
  36. enum ICMPType {
  37.     icmpNetUnreach = 0,
  38.     icmpHostUnreach = 1,
  39.     icmpProtocolUnreach = 2,
  40.     icmpPortUnreach = 3,
  41.     icmpFragReqd = 4,
  42.     icmpSourceRouteFailed = 5,
  43.     icmpTimeExceeded = 6,
  44.     icmpParmProblem = 7,
  45.     icmpMissingOption = 8
  46. };
  47.  
  48. // TCP termination reason codes
  49.  
  50. enum TCPTerminReason {
  51.     tcpTermRemoteAbort = 2,
  52.     tcpTermNetFailure = 3,
  53.     tcpTermSecurityMismatch = 4,
  54.     tcpTermTimeout = 5,
  55.     tcpTermAbort = 6,
  56.     tcpTermClose = 7,
  57.     tcpTermServiceFailure = 8
  58. };
  59.  
  60. typedef enum TCPPrecedence TCPPrecedence;
  61. typedef enum ICMPType ICMPType;
  62. typedef enum TCPTerminReason TCPTerminReason;
  63.  
  64.  
  65. /*______________________________________________________________________
  66. **
  67. ** CTCPEndpoint
  68. **
  69. **    This mix-in class is provided to link your TCL classes with the TurboTCP CTCPStream
  70. **    class. View this as a “connection session” helper. In a typical application, this class
  71. **    (or a subclass of it) would be mixed with a CDocument or CDialogDirector.
  72. **
  73. **    This class does not implement any specific TCP protocol. You will need to subclass it
  74. **    to provide the behaviors specific to the protocol you are implementing. You may want
  75. **    to examine and use the CTelnetInterpreter class to see how this may be done.
  76. **
  77. **    This class provides most of the necessary behaviors for opening and closing a session.
  78. **    It provides abstract methods for receiving data and handling various TCP notifications.
  79. **
  80. **    TurboTCP 1.0 NOTE: This class replaces the CCollaborator link between CTCPStream and
  81. **    CTCPSessionDoc which was existed in version 1.0. It *must* be included in your project.
  82. **
  83. */
  84.  
  85.  
  86. class CTCPEndpoint {
  87.  
  88.     friend class CTCPStream;
  89.     friend class CTCPResolverCall;
  90.     
  91.     TCL_DECLARE_CLASS;
  92.  
  93. //----------------------
  94. //    public interface
  95. //
  96.  
  97. public:
  98.  
  99.     // constructor/destructor
  100.     
  101.                     CTCPEndpoint(unsigned short theDefaultPort,
  102.                         unsigned long recBufferSize = recReceiveSize,
  103.                         unsigned short autoReceiveSize = recAutoRecSize,
  104.                         unsigned short autoReceiveNum = recAutoRecNum,
  105.                         Boolean doUseCName = TRUE);         // TurboTCP 2.0: note new parameter sequence
  106.     virtual             ~CTCPEndpoint();
  107.  
  108.  
  109.     // opening/closing session
  110.  
  111.     virtual void        OpenUserHost(char* theHostName, unsigned short theDefaultPort,
  112.                         Boolean allowPortChange);
  113.     virtual void        OpenHost(unsigned long remoteHostIP, unsigned short remoteHostPort);
  114.     virtual void        Listen(unsigned long remoteHostIP, unsigned short remoteHostPort);
  115.     virtual Boolean        LocalClose(Boolean quitting);
  116.     Boolean            SessionEstablished();
  117.  
  118.  
  119.     // sending data
  120.  
  121.     void                SendBfrCpy(const void* theData, unsigned short theDataSize);
  122.     void                SendBfrNoCpy(const void* theData, unsigned short theDataSize, Boolean disposeWhenDone,
  123.                         Boolean notifyWhenDone);
  124.  
  125.     void                SendChar(const char theChar);
  126.     void                SendCString(const char* theString);
  127.     void                SendPString(const unsigned char* theString);
  128.  
  129.     void                SetNextPush();
  130.     void                SetNextUrgent();
  131.  
  132.     CTCPEndpoint& operator << (CTCPEndpoint& (*_F)(CTCPEndpoint&))
  133.         { return ((*_F)(*this)); }
  134.     friend inline CTCPEndpoint& operator << (CTCPEndpoint& s, const char* v)
  135.         { s.SendCString(v); return s; }
  136.     friend inline CTCPEndpoint& operator << (CTCPEndpoint& s, const unsigned char* v)
  137.         { s.SendPString(v); return s; }
  138.     friend inline CTCPEndpoint& operator << (CTCPEndpoint& s, char v)
  139.         { s.SendChar(v); return s; }
  140.  
  141.  
  142.     
  143.     // configuration methods
  144.  
  145.     void                SetDefaultPort(unsigned short newDefaultPort);
  146.     void                SetLocalHostPort(unsigned short newLocalPort);
  147.     void                SetOpenTimeout(unsigned short openMaxSeconds);
  148.     void                SetListenTimeout(unsigned short openMaxSeconds);
  149.     void                SetTypeOfService(Boolean lowDelay, Boolean highThroughput, Boolean highReliability);
  150.     void                SetPrecedence(TCPPrecedence newPrecedence);
  151.     void                SetDontFragment(Boolean newDontFragment);
  152.     void                SetTimeToLive(unsigned short newTimeToLive);
  153.     void                SetSecurity(unsigned short newSecurity);
  154.  
  155.     
  156.     // selectors
  157.  
  158.     void                GetRemoteHostName(char* hostStringBfr);
  159.     unsigned long        GetRemoteHostIP();
  160.     unsigned short        GetRemoteHostPort();
  161.     unsigned long        GetLocalHostIP();
  162.     unsigned short        GetLocalHostPort();
  163.     unsigned short        GetDefaultPort();
  164.  
  165.  
  166. //----------------------
  167. //    protected interface
  168. //
  169.  
  170. protected:
  171.  
  172.     // state change notifications
  173.  
  174.     virtual void        RemoteClose();
  175.     virtual void        StateChanged();
  176.  
  177.  
  178.     // document/window naming
  179.  
  180.     virtual void        SetWindowTitle(Str255 newTitle) {}        // Override to set the window title to the given string.
  181.     virtual void        GetFileName(Str255 theName)            // Override to return the file name of the session.
  182.                         { theName[0] = '\0'; }
  183.  
  184.  
  185.     // error handling
  186.  
  187.     virtual short        TCPErrorAlert(OSErr err, long message, short alertID, short parm3);
  188.  
  189.  
  190.     // TCP/DNR notification routines — called by CTCPStream and CTCPResolverCall
  191.     //    Override some or all of these methods to respond to events as needed.
  192.  
  193.     virtual void        HandleDataArrived(void* theData, unsigned short theDataSize, Boolean isUrgent) = 0;
  194.                             // ** MUST OVERRIDE **
  195.  
  196.     virtual void        HandleClosed() {}
  197.     virtual void        HandleClosing(Boolean remoteClosing);
  198.     virtual void        HandleDataSent(void* theData, unsigned short theDataSize) {}
  199.     virtual void        HandleICMP(ICMPType reportType, unsigned short optionalAddlInfo,
  200.                                 void* optionalAddlInfoPtr) {}
  201.     virtual void        HandleOpened();
  202.     virtual void        HandleOpenFailed(OSErr theResultCode);
  203.     virtual void        HandleSendFailed(void* theData, unsigned short theDataSize, OSErr theResultCode) {}
  204.     virtual void        HandleTCPError(OSErr theResultCode, short theCsCode);
  205.     virtual void        HandleTerminated(TCPTerminReason terminReason, Boolean aboutToDispose);
  206.     virtual void        HandleTimeout() {}
  207.     virtual void        HandleUnexpectedData() {}
  208.     virtual void        HandleUrgentBegin() {}
  209.  
  210.     virtual void        HandleStrToAddr(struct hostInfo* theHostInfo);
  211.     virtual void        HandleAddrToName(struct hostInfo* theHostInfo);
  212.     virtual void        HandleHInfo(struct returnRec* theHInfo) {}
  213.     virtual void        HandleMXInfo(struct returnRec* theMXInfo) {}
  214.  
  215.  
  216.     // configuration fields
  217.     //    Your subclass may change these variables to customize behaviors.
  218.     
  219.     Boolean            useCName;                // get host’s canonical name
  220.     Boolean            goAwayOnClose;            // close window when session closes
  221.     Boolean            showFileName;                // show filename in window titles
  222.     Boolean            showHostName;                // show host name in window titles
  223.     short            untitledNumber;            // serial number for untitled documents
  224.  
  225.  
  226. //----------------------
  227. //    protected interface (TEMPORARY)
  228. //        WARNING: these members will become private VERY SOON!
  229. //        Do your best to get rid of references to these members!
  230. //
  231.  
  232. protected:
  233.  
  234.     // informational fields
  235.     //    DO NOT change these values!
  236.  
  237.     CTCPStream*        itsStream;                // TCP stream for this document
  238.     CTCPResolverCall*    itsResolver;                // TCP resolver for this document
  239.     Boolean            pendingOpenByName;            // OpenUserHost has been issued
  240.     unsigned short        pendingPortNumber;            // port number for OpenUserHost
  241.     Boolean            closeAndQuit;                // closing windows to quit
  242.     
  243.     char                hostCName[256];            // canonical name of host
  244.                                             //    if cnames not requested, is user’s name
  245.     unsigned long        hostAddress;                // IP address of host
  246.     unsigned short        defaultPort;                // default port number
  247.     unsigned short        actualPort;                // current port number
  248.     unsigned short        localPort;                    // default local port number
  249.  
  250. };
  251.  
  252. //-----------------------------------
  253. //    manipulators for CTCPEndpoint
  254. //
  255.  
  256. CTCPEndpoint& endl(CTCPEndpoint& ep);
  257. CTCPEndpoint& local_IP(CTCPEndpoint& ep);
  258. CTCPEndpoint& remote_IP(CTCPEndpoint& ep);
  259. CTCPEndpoint& push(CTCPEndpoint& ep);
  260. CTCPEndpoint& urgent(CTCPEndpoint& ep);
  261.